#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#include <spdlog/spdlog.h>
#include <signal.h>
#include <stdio.h>

void set_loglevel(int signum);
int get_level();

int main(void)
{
    signal(SIGUSR1, set_loglevel);
    spdlog::set_level(spdlog::level::trace);
    while(1){
        SPDLOG_TRACE("trace");
        SPDLOG_DEBUG("debug");
        SPDLOG_INFO("info");
    }
    return 0;
}

void set_loglevel(int signum)
{
    int level = get_level();
    spdlog::level::level_enum log_level;
    switch(level){
        case 0:
            log_level = spdlog::level::trace;
            break;
        case 1:
            log_level = spdlog::level::debug;
            break;
        case 2:
            log_level = spdlog::level::info;
            break;
        case 3:
            log_level = spdlog::level::warn;
            break;
        case 4:
            log_level = spdlog::level::err;
            break;
        case 5:
            log_level = spdlog::level::critical;
            break;
    }
    SPDLOG_TRACE("trace {0}", log_level);
    spdlog::set_level(log_level);
    spdlog::flush_on(log_level);
}

int get_level()
{
    FILE *fp = fopen("loglevel", "r");
    int level = fgetc(fp);
    fclose(fp);
    return level - 48;
}


